package org;

import java.sql.*;

public class DbBean {
    private Statement stmt = null;
    private Connection conn = null;
    ResultSet rs = null;

    public DbBean() {
    };

    public void OpenConnection() {
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            String url="jdbc:sqlserver://localhost:1433;DatabaseName=eu";
            String user="sa";
            String password="1";
            conn = DriverManager.getConnection(url, user, password);
        } catch (ClassNotFoundException e) {
            System.err.println(e.getMessage());
        } catch (SQLException e) {
            System.err.println(e.getMessage());
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }

    public ResultSet executeQuery(String sql) {
        rs = null;
        try {
            stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                    ResultSet.CONCUR_UPDATABLE);
            rs = stmt.executeQuery(sql);
        } catch (SQLException e) {
            System.err.println(e.getMessage());
        }
        return rs;
    }


    public int excuteUpdate(String sql) {
        int n = 0;
        try {
            stmt = conn.createStatement();
            n = stmt.executeUpdate(sql);
        } catch (Exception e) {
            System.err.println(e.toString());
        }
        return n;
    }

    public void closeConnection() {
        try {
            if (rs != null)
                rs.close();
        } catch (SQLException e) {
            System.err.println(e.getMessage());
        }
        try {
            if (stmt != null)
                stmt.close();
        } catch (SQLException e) {
            System.err.println(e.getMessage());
        }
        try {
            if (conn != null)
                conn.close();
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }
}
